package org.lhh.server.core.codec.database;

import org.apache.commons.lang3.StringUtils;
import org.lhh.server.common.utils.CollectionUtils;
import org.pentaho.di.core.database.PartitionDatabaseMeta;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PartitionDatabaseCodec {
    private String partitionId;
    private String hostname;
    private String port;
    private String databaseName;
    private String username;
    private String password;

    public static List<PartitionDatabaseCodec>  decode(PartitionDatabaseMeta[] clusterInformation){
        List<PartitionDatabaseCodec> partitionDatabaseList = new LinkedList<>();
        if (clusterInformation != null) {
            for (PartitionDatabaseMeta partitionDatabaseMeta : clusterInformation) {
                PartitionDatabaseCodec partitionDatabase = new PartitionDatabaseCodec();
                partitionDatabase.setPartitionId(partitionDatabaseMeta.getPartitionId());
                partitionDatabase.setHostname(partitionDatabaseMeta.getHostname());
                partitionDatabase.setPort(partitionDatabaseMeta.getPort());
                partitionDatabase.setDatabaseName(partitionDatabaseMeta.getDatabaseName());
                partitionDatabase.setUsername(partitionDatabaseMeta.getUsername());
                partitionDatabase.setPassword(partitionDatabaseMeta.getPassword());
                partitionDatabaseList.add(partitionDatabase);
            }
        }
        return partitionDatabaseList;
    }

    public static PartitionDatabaseMeta[] encode(List<PartitionDatabaseCodec> partitionDatabaseList){
        List<PartitionDatabaseMeta> partitionDatabaseMetaArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(partitionDatabaseList)) {
            for (PartitionDatabaseCodec partitionDatabase : partitionDatabaseList) {
                PartitionDatabaseMeta partitionDatabaseMeta = new PartitionDatabaseMeta();
                if (StringUtils.isEmpty(partitionDatabase.getPartitionId())) {
                    continue;
                }
                partitionDatabaseMeta.setPartitionId(partitionDatabase.getPartitionId());
                partitionDatabaseMeta.setHostname(partitionDatabase.getHostname());
                partitionDatabaseMeta.setPort(partitionDatabase.getPort());
                partitionDatabaseMeta.setDatabaseName(partitionDatabase.getDatabaseName());
                partitionDatabaseMeta.setUsername(partitionDatabase.getUsername());
                partitionDatabaseMeta.setPassword(partitionDatabase.getPassword());
                partitionDatabaseMetaArrayList.add(partitionDatabaseMeta);
            }
        }
        return partitionDatabaseMetaArrayList.toArray(new PartitionDatabaseMeta[0]);
    }

    public String getPartitionId() {
        return partitionId;
    }

    public void setPartitionId(String partitionId) {
        this.partitionId = partitionId;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
